import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import router from './router'
import ElementUI from 'element-ui'
import './css/reset.css'
import 'element-ui/lib/theme-chalk/index.css'
import './lib/navIcon/iconfont.css'
import './lib/navIcon/iconfont.ttf'
import axios from 'axios'
import store from './store.js'
Vue.use(VueRouter)
Vue.use(ElementUI)

Vue.prototype.$axios = axios
Vue.prototype.$url = 'http://127.0.0.1:8080/'
Vue.prototype.$url1 = 'http://127.0.0.1:8081/'
Vue.prototype.$url2 = 'http://127.0.0.1:3000/'
// axios.defaults.baseURL = 'http://127.0.0.1:8080/'
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
axios.interceptors.request.use(config=>{
    let token =localStorage.getItem("token")
    //判断请求地址是否=login | register 如果为真则放行，否则拦截添加token
    let reg = /(\/login)|(\/register)/
    if(reg.test(config.url)) return config
    if(token){
        config.headers['token']=token
        return config
    }else{
        this.$message.warning("登录状态已失效，请重新登录")
    }
},error => {
    this.$message.error('网络异常')
})
axios.interceptors.response.use(res=>{
    if(res.status === 407){
        return this.$message.error("登录状态已失效，请重新登录")
    }
    let token =res.data.tk
    if(token){
        localStorage.setItem("token",token)
    }
    return res
},error => {
    this.$message.error('网络异常')
})
//全局表单验证方法
Vue.prototype.$verifyForm= function(params) {
    for(let key in params){
        if(params[key] != '0' && !params[key]){
            this.$message.warning('请完善数据!');
            return false; // 终止程序
        }
    }
    return true;
}

//全局手机号验证方法
Vue.prototype.$verifyPhone = function (phone) {
    if(!(/^1[3456789]\d{9}$/.test(phone))){
        this.$message.warning('手机号码格式有问题')
        return false
    }
    return true
}


new Vue({
    el:'#app',
    methods:{
    },
    created(){
    },
    render:c => c(App),
    router,store
})
